package com.nedu.android_ui_16;

import com.nedu.popujar.PopuItem;
import com.nedu.popujar.PopuJar;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.Toast;

public class ListDemoActivity extends Activity {
	/**
	 * Listview selected row
	 */
	private int mSelectedRow = 0;
	
	/**
	 * Right arrow icon on each listview row
	 */
	private ImageView mMoreIv = null;
	
	private static final int ID_USER = 1;
	private static final int ID_GROUP = 2;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.list);
        
        ListView mList 			= (ListView) findViewById(R.id.l_list);
        
        NewQAAdapter adapter 	= new NewQAAdapter(this);
        
        final String[] data 	= {"Test 01", "Test 02", "Test 03", "Test 04", "Test 05", "Test 06", "Test 07", "Test 08",
        					   	  "Test 09", "Test 10"};
        
        adapter.setData(data);
        mList.setAdapter(adapter);
        
        PopuItem addItem 		= new PopuItem(ID_USER, "user", getResources().getDrawable(R.drawable.child_image));
		PopuItem acceptItem 	= new PopuItem(ID_GROUP, "group", getResources().getDrawable(R.drawable.user_group));

		
		final PopuJar mPopuJar 	= new PopuJar(this,PopuJar.HORIZONTAL);
		
		mPopuJar.addPopuItem(addItem);
		mPopuJar.addPopuItem(acceptItem);

		
		//setup the action item click listener
		mPopuJar.setOnPopuItemClickListener(new PopuJar.OnPopuItemClickListener() {
			@Override
			public void onItemClick(PopuJar PopuJar, int pos, int actionId) {
				PopuItem PopuItem = PopuJar.getPopuItem(pos);
				
				if (actionId == ID_USER) { //Add item selected
					Toast.makeText(getApplicationContext(), "Add item selected on row " + mSelectedRow, Toast.LENGTH_SHORT).show();
				} else {
					Toast.makeText(getApplicationContext(), PopuItem.getTitle() + " item selected on row " 
							+ mSelectedRow, Toast.LENGTH_SHORT).show();
				}	
			}
		});
		
		//setup on dismiss listener, set the icon back to normal
		mPopuJar.setOnDismissListener(new PopupWindow.OnDismissListener() {			
			@Override
			public void onDismiss() {
				mMoreIv.setImageResource(R.drawable.ic_list_more);
			}
		});
		
		mList.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				mSelectedRow = position; //set the selected row
				
				mPopuJar.show(view);
				
				//change the right arrow icon to selected state 
				mMoreIv = (ImageView) view.findViewById(R.id.i_more);
				mMoreIv.setImageResource(R.drawable.ic_list_more_selected);
			}
		});
    }
}